package com.cisite.cont

import org.springframework.dao.DataIntegrityViolationException
import com.cisite.manager.Manager;
class ChannelController {

    static allowedMethods = [save: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [channelInstanceList: Channel.list(params), channelInstanceTotal: Channel.count()]
    }

    def create() {
        [channelInstance: new Channel(params)]
    }

    def save() {
        def channelInstance = new Channel(params)
		def manager = new Manager();
		manager.id = 1;
		channelInstance.manager = manager;
		
        if (!channelInstance.save(flush: true)) {
            render(view: "create", model: [channelInstance: channelInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'channel.label', default: 'Channel'), channelInstance.id])
        redirect(action: "show", id: channelInstance.id)
    }

    def show() {
        def channelInstance = Channel.get(params.id)
        if (!channelInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'channel.label', default: 'Channel'), params.id])
            redirect(action: "list")
            return
        }

        [channelInstance: channelInstance]
    }

    def edit() {
        def channelInstance = Channel.get(params.id)
        if (!channelInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'channel.label', default: 'Channel'), params.id])
            redirect(action: "list")
            return
        }

        [channelInstance: channelInstance]
    }

    def update() {
        def channelInstance = Channel.get(params.id)
        channelInstance.properties = params
		def manager = new Manager();
		manager.id = 1;
		channelInstance.manager = manager;
		
        if (!channelInstance.save(flush: true)) {
            render(view: "edit", model: [channelInstance: channelInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'channel.label', default: 'Channel'), channelInstance.id])
        redirect(action: "show", id: channelInstance.id)
    }

    def delete() {
        def channelInstance = Channel.get(params.id)
        if (!channelInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'channel.label', default: 'Channel'), params.id])
            redirect(action: "list")
            return
        }

        try {
            channelInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'channel.label', default: 'Channel'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'channel.label', default: 'Channel'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
